package com.ktg.mes.md.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktg.common.core.domain.APIResult;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.system.strategy.AutoCodeUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.core.domain.Result;
import com.ktg.mes.md.domain.MesScalesOrder;
import com.ktg.mes.md.service.IMesScalesOrderService;
import com.ktg.common.utils.poi.ExcelUtil;

/**
 * 销售订单Controller
 *
 * @author byteco
 * @date 2022-09-03
 */
@RestController
@RequestMapping("/md/mesScalesOrder")
public class MesScalesOrderController extends BaseController
{
    @Autowired
    private IMesScalesOrderService mesScalesOrderService;

    @Autowired
    private AutoCodeUtil autoCodeUtil;


    /**
     * 查询销售订单列表
     */
    @PreAuthorize("@ss.hasPermi('md:mesScalesOrder:list')")
    @GetMapping("/list")
    public Result<IPage<MesScalesOrder>> list(MesScalesOrder mesScalesOrder,
                                              @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                              @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                              HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("查询销售订单列表",req,null);
        try {
            Page<MesScalesOrder> page = new Page<MesScalesOrder>(pageNo, pageSize);
            IPage<MesScalesOrder> pageList = mesScalesOrderService.queryPageList(mesScalesOrder,page);
            return apiLog.ok(pageList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 导出销售订单列表
     */
    @PreAuthorize("@ss.hasPermi('md:mesScalesOrder:export')")
    @Log(title = "销售订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesScalesOrder mesScalesOrder,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("导出销售订单列表",req,mesScalesOrder);
        try {
            List<MesScalesOrder> list = mesScalesOrderService.queryList(mesScalesOrder);
            ExcelUtil<MesScalesOrder> util = new ExcelUtil<MesScalesOrder>(MesScalesOrder.class);
            util.exportExcel(response, list, "销售订单数据");
            apiLog.ok(null);
        }catch (Exception e){
            apiLog.error(e);
        }
    }

    /**
     * 获取销售订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('md:mesScalesOrder:query')")
    @GetMapping(value = "/{scalesOrderId}")
    public Result<MesScalesOrder> getInfo(@PathVariable("scalesOrderId") String scalesOrderId,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("获取销售订单详细信息",req,null);
        try {
            return apiLog.ok(mesScalesOrderService.getInfo(scalesOrderId));
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 新增销售订单
     */
    @PreAuthorize("@ss.hasPermi('md:mesScalesOrder:add')")
    @Log(title = "销售订单", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<String> add(@RequestBody MesScalesOrder mesScalesOrder,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("新增销售订单",req,mesScalesOrder);
        try {
            String orCode = autoCodeUtil.genSerialCode("SCALES_ORDER",null);
            mesScalesOrder.setScalesOrderCode(orCode);
            mesScalesOrder.setSalesman(SecurityUtils.getUsername());
            mesScalesOrderService.insert(mesScalesOrder);
            return apiLog.ok(mesScalesOrder);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 修改销售订单
     */
    @PreAuthorize("@ss.hasPermi('md:mesScalesOrder:edit')")
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<String> edit(@RequestBody MesScalesOrder mesScalesOrder,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("修改销售订单",req,mesScalesOrder);
        try {
            mesScalesOrderService.update(mesScalesOrder);
            return apiLog.ok("编辑成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 删除销售订单
     */
    @PreAuthorize("@ss.hasPermi('md:mesScalesOrder:remove')")
    @Log(title = "销售订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scalesOrderIds}")
    public Result<String> remove(@PathVariable String[] scalesOrderIds,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("删除销售订单",req,null);
        try {
            mesScalesOrderService.delete(scalesOrderIds);
            return apiLog.ok("删除成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }

    }
}
